package com.douing.ad.dsp.bidding.utils;

import org.slf4j.ILoggerFactory;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import java.util.List;

import javax.annotation.PostConstruct;

import ch.qos.logback.classic.Level;
import ch.qos.logback.classic.LoggerContext;

@Service
public class LogLevelChangeService {
  private static final Logger theLogger = LoggerFactory.getLogger(LogLevelChangeService.class);
  private static final String LOG_CURRENT_LEVEL_KEY = "log.current.level";
  private static final String LOG_PREFIX_KEY = "log.prefix";
  private static final String ENABLE_CHANGE_ROOT_LOGGER_KEY = "enable.change.root.logger";
  @Value("${"+LOG_PREFIX_KEY+"}")
  private volatile String logPrefix;
  @Value("${"+LOG_CURRENT_LEVEL_KEY+"}")
  private volatile String logLevel;
  @Value("${"+ ENABLE_CHANGE_ROOT_LOGGER_KEY +"}")
  private  volatile String changeRootLogger;

  public void setListener() {}

  public static Logger getTheLogger() {
    return theLogger;
  }

  public String getLogPrefix() {
    return logPrefix;
  }

  public void setLogPrefix(String logPrefix) {
    this.logPrefix = logPrefix;
  }

  public String getLogLevel() {
    return logLevel;
  }

  public void setLogLevel(String logLevel) {
    this.logLevel = logLevel;
  }

  public String getChangeRootLogger() {
    return changeRootLogger;
  }

  public void setChangeRootLogger(String changeRootLogger) {
    this.changeRootLogger = changeRootLogger;
  }

  @PostConstruct
  public void init() {}
private Boolean checkEnabled(String enableStr){
  return ( UtilValidate.isNotEmpty(enableStr) && enableStr.equalsIgnoreCase
      ("True")) ? Boolean.TRUE : Boolean.FALSE;
}
  private synchronized void resetLogLevel(String logerPrefix, String level,Boolean enChangeRootlogger) {
    theLogger.warn("Logger level is [{}]", level);
    theLogger.warn("Logger loggerPrefix is [{}]", logerPrefix);
    if (logerPrefix == null || logerPrefix.isEmpty()) {
      theLogger.warn("Do NOT touch logger since config key missing: {} ", logerPrefix);
      return;
    }
    if (level == null || level.isEmpty()) {
      theLogger.warn("Do NOT touch logger since config key missing: {} ", level);
      return;
    }
    Level newLevel = Level.toLevel(level);
    theLogger.warn("Change Log Level to {}", newLevel);
    ILoggerFactory lf = LoggerFactory.getILoggerFactory();
    if (lf instanceof LoggerContext) {
      LoggerContext lc = (LoggerContext) lf;
      if (enChangeRootlogger) {
        ch.qos.logback.classic.Logger root = lc.getLogger("root");
        root.setLevel(newLevel);
        theLogger.warn("Logger {}, from: {}, to: {}", root, root.getLevel(), newLevel);
      }
      List<ch.qos.logback.classic.Logger> loggers = lc.getLoggerList();
      for (ch.qos.logback.classic.Logger log : loggers) {
        String name = log.getName();
        if (name.startsWith(logerPrefix)) {
          log.setLevel(newLevel);
        }
      }
    }
    else theLogger.error("Can only change theLogger level for logback");
  }
}